package com.xsd.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Description: 异步回调
 * @Author: zhoufuzong
 * @Created: 2021-01-03 18:42:12
 * @Version: 1.1.0
 */

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println(Thread.currentThread().getName() + "\t 没有返回，update mysql ok");
//        });
//        completableFuture.get();
//
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 有返回，insert mysql ok");
            int age =10/0;
            return 1024;
        });

        System.out.println(completableFuture1.whenComplete((t, u) -> {
            System.out.println("*****t: " + t);  // 正常执行的数据
            System.out.println("*****u: " + u);  // 异常信息
        }).exceptionally(f -> {
            System.out.println("***** exception: " + f.getMessage());
            return 4444;
//            return null;
        }).get());  // 拿到异步回调执行结果


    }
}
